package com.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
public class ImageUploadConfig implements WebMvcConfigurer {
    private static final String BASE_DIR = System.getProperty("user.dir");

    private static final String STATIC_PATH = "src/main/resources/static";
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        Path imagePath = Paths.get(BASE_DIR, STATIC_PATH, "/images");
        registry.addResourceHandler("/images/**").addResourceLocations(imagePath.toUri().toString());
        Path filePath = Paths.get(BASE_DIR, STATIC_PATH, "/tb_file");
        registry.addResourceHandler("/tb_file/**").addResourceLocations(filePath.toUri().toString());
    }
//    private static final String BASE_DIR = System.getProperty("user.dir");
//
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        // 设置上传图片的路径为 JAR 包所在目录下的 images 文件夹
//        Path imagePath = Paths.get(BASE_DIR, "/images");
//        registry.addResourceHandler("/images/**").addResourceLocations("file:" + imagePath.toAbsolutePath().toString() + "/");
//
//        Path filePath = Paths.get(BASE_DIR, "/tb_file");
//        registry.addResourceHandler("/tb_file/**").addResourceLocations(filePath.toUri().toString());
//    }
}
